package hello.controller;

import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.OutputStream;

/**
 * <br>
 * 版权: Copyright (c) 2011-2018<br>
 * 公司: 活力天汇<br>
 *
 * @author: 刘德咏<br>
 * @date: 2018-04-25<br>
 */
@Controller
public class FileInputStreamController {
    private Logger logger = LoggerFactory.getLogger(FileInputStreamController.class);

    // http://localhost:8080/zipFile
    @RequestMapping("/zipFile")
    public void zipFile(HttpServletResponse response) {
        OutputStream output;
        FileInputStream input;
        // 服务器端
        try {
            output = response.getOutputStream();
            input = new FileInputStream(new ClassPathResource("file/zipFIle.zip").getFile());
            byte[] bts = new byte[1024];
            int len;
            while((len=input.read(bts))!=-1){
                output.write(bts,0,len);
            }
        } catch (Exception e){
            logger.error("{}", e);
        }
    }
}
